//Copyright (C) 2012 Chirila Alexandru
//This file is part of Robo-GoldMiner
//
//Robo-GoldMiner is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//Robo-GoldMiner is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with Robo-GoldMiner.  If not, see <http://www.gnu.org/licenses/>.

package gold;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * This command changes the current room color to the inputed one.
 * Sintax: color {name of color} <br />
 * Possible names of colors: "red", "green", "blue" 
 * @author Alexandru Chirila
 */
public class CommandColor implements UndoCommand, Serializable {

	private static final long serialVersionUID = 4467142769850468981L;
	
	/**
	 * Color to which the room should be changed to.
	 */
	private String color;
	
	/**
	 * Previous color for undo.
	 */
	private String previousColor;
	
	/**
	 * The command is aware and uses the model.
	 */
	private Maze m;
	
	public CommandColor(Maze m){
		this.m = m;
	}
	
	@Override
	public boolean execute() throws EndGame, ParseException {
		previousColor = m.getCurrentColorExternalRepr();
		return m.changeColor(getColor());
	}
	
	@Override
	public void undo() throws ParseException {
		m.setCurrentColor(previousColor);
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	@Override
	public UndoCommand copy() {
		CommandColor clone = new CommandColor(m);
		
		clone.previousColor = this.previousColor;
		clone.color = this.color;
		
		return clone;
	}

	private void writeObject(ObjectOutputStream stream) throws IOException {
		stream.defaultWriteObject();
	}

	private void readObject(ObjectInputStream stream) throws IOException,
			ClassNotFoundException {
		stream.defaultReadObject();
	}

}
